package com.reebake.ideal.security.oauth2.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.mybatis.core.PageResult;
import com.reebake.ideal.security.oauth2.entity.ClientRegistrationEntity;
import com.reebake.ideal.security.oauth2.service.ClientRegistrationService;
import com.reebake.ideal.security.oauth2.vo.ClientRegistrationQueryParamVO;
import com.reebake.ideal.security.oauth2.vo.ClientRegistrationVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/oauth2/client-registration")
public class ClientRegistrationController {
    private final ClientRegistrationService clientRegistrationService;

    @PostMapping("page")
    public PageResult<ClientRegistrationVO> page(@RequestBody ClientRegistrationQueryParamVO queryParam) {
        LambdaQueryWrapper<ClientRegistrationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getClientId()), ClientRegistrationEntity::getClientId, queryParam.getClientId());
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getClientName()), ClientRegistrationEntity::getClientName, queryParam.getClientName());

        queryParam = clientRegistrationService.page(queryParam, queryWrapper);

        return queryParam.to(ClientRegistrationVO.class);
    }

    @PostMapping("save")
    public void save(@RequestBody ClientRegistrationVO clientRegistrationVO) {
        clientRegistrationService.saveOrUpdate(BeanUtil.copyProperties(clientRegistrationVO, ClientRegistrationEntity.class));
    }

    @PostMapping("remove-by-ids")
    public void removeById(@RequestBody List<String> ids) {
        clientRegistrationService.removeByIds(ids);
    }
}
